package com.todo.biz.application.convert;

import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.todo.biz.application.entity.bo.*;
import com.todo.biz.application.entity.condition.CommentConditionBO;
import com.todo.biz.application.entity.condition.FeedbackConditionBO;
import com.todo.biz.common.enums.*;
import com.todo.biz.iface.backend.request.QueryFeedbackByConditionReq;
import com.todo.biz.iface.backend.response.QueryFeedbackByConditionResp;
import com.todo.biz.iface.backend.response.QueryFeedbackByIdResp;
import com.todo.biz.infrastructure.entity.condition.CommentCondition;
import com.todo.biz.infrastructure.entity.condition.FeedbackCondition;
import com.todo.biz.infrastructure.entity.po.BizAssociatePO;
import com.todo.biz.infrastructure.entity.po.BizCounterPO;
import com.todo.biz.infrastructure.entity.po.CommentPO;
import com.todo.biz.infrastructure.entity.po.FeedbackPO;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.common.utils.TodoCommonUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Date;
import java.util.stream.Collectors;


/**
 * @author azhebuxing
 * @date 2024/5/7 14:42
 * @description
 */
public class BizBusinessConvert {

    public static FeedbackPO converToFeedbackPO(FeedbackBO feedbackBO) {
        if (feedbackBO == null) {
            return null;
        }
        FeedbackPO feedbackPO = new FeedbackPO();
        feedbackPO.setFeedbackType(feedbackBO.getFeedbackType() == null ? null : feedbackBO.getFeedbackType().getType());
        feedbackPO.setBusinessId(feedbackBO.getBusinessId());
        feedbackPO.setTitle(feedbackBO.getTitle());
        feedbackPO.setContent(feedbackBO.getContent());
        feedbackPO.setExtra(JSONObject.toJSONString(feedbackBO.getExtra()));
        feedbackPO.setStatus(feedbackBO.getStatus() == null ? null :feedbackBO.getStatus().getType());
        feedbackPO.setIsSolve(feedbackBO.getIsSolve());
        feedbackPO.setCreateUser(feedbackBO.getCreateUser());
        feedbackPO.setBusinessScene(feedbackBO.getBusinessScene() == null ? null :feedbackBO.getBusinessScene().getScene());
        feedbackPO.setId(feedbackBO.getId() == null ? TodoCommonUtils.buildLongGuid() : feedbackBO.getId());
        feedbackPO.setIsDelete(0);
        feedbackPO.setCreateTime(new Date());
        feedbackPO.setUpdateTime(new Date());
        return feedbackPO;
    }

    public static FeedbackPO replyconverToFeedbackPO(FeedbackReplyBO feedbackBO) {
        if (feedbackBO == null) {
            return null;
        }
        FeedbackPO feedbackPO = new FeedbackPO();
        feedbackPO.setReplyContent(feedbackBO.getReplyContent());
        feedbackPO.setReplyUser(feedbackBO.getReplyUser());
        feedbackPO.setReplyTime(feedbackBO.getReplyTime());
        feedbackPO.setId(feedbackBO.getId());
        return feedbackPO;
    }

    public static FeedbackDetailBO convertToFeedbackDetailBO(FeedbackPO feedbackPO) {
        if (feedbackPO == null) {
            return null;
        }
        FeedbackDetailBO feedbackDetailBO = new FeedbackDetailBO();
        feedbackDetailBO.setReplyContent(feedbackPO.getReplyContent());
        feedbackDetailBO.setReplyUser(feedbackPO.getReplyUser());
        feedbackDetailBO.setReplyTime(feedbackPO.getReplyTime());
        feedbackDetailBO.setExtra(JSONObject.parseObject(feedbackPO.getExtra(), FeedbackDetailBO.Extra.class));
        feedbackDetailBO.setStatus(FeedbackStatusEnum.getByType(feedbackPO.getStatus()));
        feedbackDetailBO.setIsSolve(feedbackPO.getIsSolve());
        feedbackDetailBO.setId(feedbackPO.getId());
        feedbackDetailBO.setBusinessScene(BusinessSceneEnum.getBusinessSceneEnumByScene(feedbackPO.getBusinessScene()));
        feedbackDetailBO.setBusinessId(feedbackPO.getBusinessId());
        feedbackDetailBO.setTitle(feedbackPO.getTitle());
        feedbackDetailBO.setContent(feedbackPO.getContent());
        feedbackDetailBO.setCreateUser(feedbackPO.getCreateUser());
        feedbackDetailBO.setFeedbackType(FeedbackTypeEnum.getByType(feedbackPO.getFeedbackType()));
        return feedbackDetailBO;
    }

    public static FeedbackBO convertToFeedbackBO(FeedbackPO feedbackPO) {
        if (feedbackPO == null) {
            return null;
        }
        FeedbackBO feedbackBO = new FeedbackBO();
        feedbackBO.setExtra(JSONObject.parseObject(feedbackPO.getExtra(), FeedbackBO.Extra.class));
        feedbackBO.setStatus(FeedbackStatusEnum.getByType(feedbackPO.getStatus()));
        feedbackBO.setIsSolve(feedbackPO.getIsSolve());
        feedbackBO.setId(feedbackPO.getId());
        feedbackBO.setBusinessScene(BusinessSceneEnum.getBusinessSceneEnumByScene(feedbackPO.getBusinessScene()));
        feedbackBO.setBusinessId(feedbackPO.getBusinessId());
        feedbackBO.setTitle(feedbackPO.getTitle());
        feedbackBO.setContent(feedbackPO.getContent());
        feedbackBO.setCreateUser(feedbackPO.getCreateUser());
        feedbackBO.setFeedbackType(FeedbackTypeEnum.getByType(feedbackPO.getFeedbackType()));
        return feedbackBO;
    }


    public static FeedbackCondition convertToFeedbackCondition(FeedbackConditionBO feedbackConditionBO) {
        if (feedbackConditionBO == null) {
            return null;
        }
        FeedbackCondition feedbackCondition = new FeedbackCondition();
        feedbackCondition.setPageSize(feedbackConditionBO.getPageSize());
        feedbackCondition.setPageIndex(feedbackConditionBO.getPageIndex());
        feedbackCondition.setBusinessScene(feedbackConditionBO.getBusinessScene() == null ? null : feedbackConditionBO.getBusinessScene().getScene());
        feedbackCondition.setBusinessId(feedbackConditionBO.getBusinessId());
        feedbackCondition.setFeedbackType(feedbackConditionBO.getFeedbackType() == null ? null : feedbackConditionBO.getFeedbackType().getType());
        feedbackCondition.setStatus(feedbackConditionBO.getStatus() == null ? null : feedbackConditionBO.getStatus().getType());
        feedbackCondition.setIsSolve(feedbackConditionBO.getIsSolve());
        feedbackCondition.setTitle(feedbackConditionBO.getTitle());
        feedbackCondition.setCreateUser(feedbackConditionBO.getCreateUser());
        feedbackCondition.setCurrentIndex(feedbackConditionBO.getCurrentIndex());
        feedbackCondition.setReplyUser(feedbackConditionBO.getReplyUser());
        return feedbackCondition;
    }

    public static BizCounterPO convertToBizCounterPO(BizCounterBO bizCounterBO) {
        if (bizCounterBO == null) {
            return null;
        }
        BizCounterPO bizCounterPO = new BizCounterPO();
        bizCounterPO.setCounterType(bizCounterBO.getCounterType().getType());
        bizCounterPO.setBusinessId(bizCounterBO.getBusinessId());
        bizCounterPO.setCount(bizCounterBO.getCount());
        bizCounterPO.setBusinessScene(bizCounterBO.getBusinessScene().getScene());
        bizCounterPO.setExtra(JSONObject.toJSONString(bizCounterBO.getExtra()));
        bizCounterPO.setId(bizCounterBO.getId());
        return bizCounterPO;
    }

    public static BizCounterBO convertToBizCounterBO(BizCounterPO bizCounterPO) {
        if (bizCounterPO == null) {
            return null;
        }
        BizCounterBO bizCounterBO = new BizCounterBO();
        bizCounterBO.setCounterType(CounterTypeEnum.getByType(bizCounterPO.getCounterType()));
        bizCounterBO.setBusinessId(bizCounterPO.getBusinessId());
        bizCounterBO.setCount(bizCounterPO.getCount());
        bizCounterBO.setBusinessScene(BusinessSceneEnum.getBusinessSceneEnumByScene(bizCounterPO.getBusinessScene()));
        bizCounterBO.setExtra(JSONObject.parseObject(bizCounterPO.getExtra(), BizCounterBO.Extra.class));
        bizCounterBO.setId(bizCounterPO.getId());
        return bizCounterBO;
    }

    public static BizAssociatePO converToBizAssociatePO(BizAssociateBO bizAssociateBO) {
        if (bizAssociateBO == null) {
            return null;
        }
        BizAssociatePO bizAssociatePO = new BizAssociatePO();
        bizAssociatePO.setBusinessScene(bizAssociateBO.getBusinessScene() == null ? null :bizAssociateBO.getBusinessScene().getScene());
        bizAssociatePO.setExtra(bizAssociateBO.getExtra() == null ? new JSONObject().toJSONString() :JSONObject.toJSONString(bizAssociateBO.getExtra()));
        bizAssociatePO.setId(bizAssociateBO.getId() == null ? TodoCommonUtils.buildLongGuid() : bizAssociateBO.getId());
        bizAssociatePO.setRelationId(bizAssociateBO.getRelationId());
        bizAssociatePO.setRelationType(bizAssociateBO.getRelationType().getType());
        bizAssociatePO.setStatus(bizAssociateBO.getStatus());
        bizAssociatePO.setUserId(bizAssociateBO.getUserId());
        bizAssociatePO.setCreateTime(new Date());
        bizAssociatePO.setUpdateTime(new Date());
        bizAssociatePO.setIsDelete(0);
        return bizAssociatePO;
    }

    public static BizAssociateBO convertToBizAssociateBO(BizAssociatePO bizAssociatePO) {
        if (bizAssociatePO == null) {
            return null;
        }
        BizAssociateBO bizAssociateBO = new BizAssociateBO();
        bizAssociateBO.setBusinessScene(BusinessSceneEnum.getBusinessSceneEnumByScene(bizAssociatePO.getBusinessScene()));
        bizAssociateBO.setExtra(JSONObject.parseObject(bizAssociatePO.getExtra(), BizAssociateBO.Extra.class));
        bizAssociateBO.setId(bizAssociatePO.getId());
        bizAssociateBO.setRelationId(bizAssociatePO.getRelationId());
        bizAssociateBO.setRelationType(AssociateRelationTypeEnum.getByType(bizAssociatePO.getRelationType()));
        bizAssociateBO.setStatus(bizAssociatePO.getStatus());
        bizAssociateBO.setUserId(bizAssociatePO.getUserId());
        return bizAssociateBO;
    }

    public static CommentPO convertToCommentPO(CommentBO commentBO) {
        if (commentBO == null) {
            return null;
        }
        CommentPO commentPO = new CommentPO();
        commentPO.setComments(commentBO.getComments());
        commentPO.setBusinessScene(commentBO.getBusinessScene().getScene());
        commentPO.setBeRepliedUser(commentBO.getBeRepliedUser());
        commentPO.setChildCount(commentBO.getChildCount());
        commentPO.setCommentImg(commentBO.getCommentImg());
        commentPO.setCommentStorey(commentBO.getCommentStorey() == null ? 1 : commentBO.getCommentStorey());
        commentPO.setCommentType(commentBO.getCommentType().getType());
        commentPO.setCommentUser(commentBO.getCommentUser());
        commentPO.setId(commentBO.getId() == null ? TodoCommonUtils.buildLongGuid() : commentBO.getId());
        commentPO.setIsHide(commentBO.getIsHide() == null ? 0 : commentBO.getIsHide());
        commentPO.setIsTop(commentBO.getIsTop() == null ? 0 : commentBO.getIsTop());
        commentPO.setLikeCount(commentBO.getLikeCount());
        commentPO.setParentId(commentBO.getParentId());
        commentPO.setIsDelete(0);
        commentPO.setUpdateTime(new Date());
        commentPO.setCreateTime(new Date());
        return commentPO;
    }

    public static CommentBO convertToCommentBO(CommentPO commentPO) {
        if (commentPO == null) {
            return null;
        }
        CommentBO commentBO = new CommentBO();
        commentBO.setComments(commentPO.getComments());
        commentBO.setBusinessScene(BusinessSceneEnum.getBusinessSceneEnumByScene(commentPO.getBusinessScene()));
        commentBO.setBeRepliedUser(commentPO.getBeRepliedUser());
        commentBO.setChildCount(commentPO.getChildCount());
        commentBO.setCommentImg(commentPO.getCommentImg());
        commentBO.setCommentStorey(commentPO.getCommentStorey());
        commentBO.setCommentType(CommentTypeEnum.getByType(commentPO.getCommentType()));
        commentBO.setCommentUser(commentPO.getCommentUser());
        commentBO.setId(commentPO.getId());
        commentBO.setIsHide(commentPO.getIsHide());
        commentBO.setIsTop(commentPO.getIsTop());
        commentBO.setLikeCount(commentPO.getLikeCount());
        commentBO.setParentId(commentPO.getParentId());
        return commentBO;
    }

    public static CommentCondition convertToCommentCondition(CommentConditionBO conditionBO) {
        if (conditionBO == null) {
            return null;
        }
        CommentCondition commentCondition = new CommentCondition();
        commentCondition.setCommentType(conditionBO.getCommentType().getType());
        commentCondition.setCommentUser(conditionBO.getCommentUser());
        commentCondition.setCurrentIndex(conditionBO.getCurrentIndex());
        commentCondition.setPageIndex(conditionBO.getPageIndex());
        commentCondition.setPageSize(conditionBO.getPageSize());
        commentCondition.setParentId(conditionBO.getParentId());
        commentCondition.setBeRepliedUser(conditionBO.getBeRepliedUser());
        return commentCondition;
    }

    public static QueryFeedbackByIdResp convertToQueryFeedbackByIdResp(FeedbackDetailBO feedbackDetailBO) {
        if (feedbackDetailBO == null) {
            return null;
        }
        QueryFeedbackByIdResp queryFeedbackByIdResp = new QueryFeedbackByIdResp();
        queryFeedbackByIdResp.setBusinessId(feedbackDetailBO.getBusinessId());
        queryFeedbackByIdResp.setBusinessScene(feedbackDetailBO.getBusinessScene().getScene());
        queryFeedbackByIdResp.setContent(feedbackDetailBO.getContent());
        queryFeedbackByIdResp.setCreateUser(feedbackDetailBO.getCreateUser());
        queryFeedbackByIdResp.setFeedbackType(feedbackDetailBO.getFeedbackType().getType());
        if (feedbackDetailBO.getExtra() != null) {
            queryFeedbackByIdResp.setFeedbackImages(feedbackDetailBO.getExtra().getImgList());
            queryFeedbackByIdResp.setUserPhone(feedbackDetailBO.getExtra().getUserPhone());
        }
        queryFeedbackByIdResp.setId(feedbackDetailBO.getId());
        queryFeedbackByIdResp.setIsSolve(feedbackDetailBO.getIsSolve());
        queryFeedbackByIdResp.setReplyContent(feedbackDetailBO.getReplyContent());
        queryFeedbackByIdResp.setReplyTime(feedbackDetailBO.getReplyTime());
        queryFeedbackByIdResp.setReplyUser(feedbackDetailBO.getReplyUser());
        queryFeedbackByIdResp.setStatus(feedbackDetailBO.getStatus().getCode());
        queryFeedbackByIdResp.setTitle(feedbackDetailBO.getTitle());
        return queryFeedbackByIdResp;
    }

    public static FeedbackConditionBO convertToFeedbackConditionBO(QueryFeedbackByConditionReq req) {
        if (req == null) {
            return null;
        }
        FeedbackConditionBO feedbackCondition = new FeedbackConditionBO();
        feedbackCondition.setBusinessId(req.getBusinessId());
        feedbackCondition.setBusinessScene(BusinessSceneEnum.getBusinessSceneEnumByScene(req.getBusinessScene()));
        feedbackCondition.setCurrentIndex(req.getCurrentIndex());
        feedbackCondition.setFeedbackType(FeedbackTypeEnum.getByType(req.getFeedbackType()));
        feedbackCondition.setIsSolve(req.getIsSolve());
        feedbackCondition.setPageIndex(req.getPageIndex());
        feedbackCondition.setPageSize(req.getPageSize());
        feedbackCondition.setReplyUser(req.getReplyUser());
        feedbackCondition.setStatus(FeedbackStatusEnum.getByType(req.getStatus()));
        feedbackCondition.setTitle(req.getTitle());
        return feedbackCondition;
    }

    public static PageResult<QueryFeedbackByConditionResp> convertToQueryFeedbackByConditionRespPage(PageResult<FeedbackBO> pageResult) {
        if (pageResult == null) {
            return PageResult.of(Lists.newArrayList(), 0, 0, 0);
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return PageResult.of(Lists.newArrayList(), pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getPageSize());
        }
        PageResult<QueryFeedbackByConditionResp> result = new PageResult<>();
        result.setCurrentIndex(pageResult.getCurrentIndex());
        result.setHasNext(pageResult.isHasNext());
        result.setPageCount(pageResult.getPageCount());
        result.setPageNum(pageResult.getPageNum());
        result.setResults(pageResult.getResults().stream().map(feedbackBO -> {
            QueryFeedbackByConditionResp queryFeedbackByConditionResp = new QueryFeedbackByConditionResp();
            queryFeedbackByConditionResp.setBusinessId(feedbackBO.getBusinessId());
            queryFeedbackByConditionResp.setBusinessScene(feedbackBO.getBusinessScene() == null ? null : feedbackBO.getBusinessScene().getScene());
            queryFeedbackByConditionResp.setFeedbackType(feedbackBO.getFeedbackType() == null ? null : feedbackBO.getFeedbackType().getType());
            queryFeedbackByConditionResp.setId(feedbackBO.getId());
            queryFeedbackByConditionResp.setIsSolve(feedbackBO.getIsSolve());
            queryFeedbackByConditionResp.setStatus(feedbackBO.getStatus() == null ? null : feedbackBO.getStatus().getCode());
            queryFeedbackByConditionResp.setTitle(feedbackBO.getTitle());
            return queryFeedbackByConditionResp;
        }).collect(Collectors.toList()));
        result.setTotal(pageResult.getTotal());
        return result;
    }
}
